package sicau.car.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import sicau.car.entity.User;
import sicau.car.service.UserService;
import sicau.car.service.impl.UserServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;


/**
 * @Author ITcz
 * @Data 2020-09-16 - 13:09
 *
 * 检查用户信息是否有效，没有则注入，注入失败则返回登录页面重新登录
 */

public class UserIntercepter extends HandlerInterceptorAdapter {

    @Autowired
    UserServiceImpl userService;


//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//                String userId = request.getHeader("userId");
//                String userName = request.getHeader("userName");
//                System.out.println("拦截到用户信息:"+userId+"    "+userName);
//                if(userId == null) {
//                    response.getWriter().write("无法获取账户信息,请重新登录");
//                    String redirectURL= URLEncoder.encode("https://authserver.sicau.edu.cn/authserver/login");
//                    response.sendRedirect(redirectURL);
//                    return false;
//                }
//                //查询本地数据库，检查用户是否是第一次登陆，如果是则加入信息到数据库
//                User user = userService.getUser(userId);
//                System.out.println("查询本地数据库 username:"+userId);
//                if(null == user){
//                    user.setMonthlyFrequency(0);
//                    user.setUsername(userId);
//                    int i = userService.insertUser(user);
//                    if(i == 0) {
//                        //新用户存入数据库，如果失败就重新登录
//                        response.getWriter().write("账户信息无法储存到服务器，请尝试重新登录");
//                        String casLogoutURL = "https://authserver.sicau.edu.cn/authserver/logout";
//                        String redirectURL=casLogoutURL+"?service="+ URLEncoder.encode("http://113.54.11.32:80/SICAU/userCar/index/index.html");
//                        response.sendRedirect(redirectURL);
//                        return false;
//                    }
//                }
//                return true;
//        }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        String userId = (String)request.getSession().getAttribute("userId");
        String userName = (String)request.getSession().getAttribute("userName");
        //查询本地数据库，检查用户是否是第一次登陆，如果是则加入信息到数据库
       /*
        *  userService.containUser如果不存在返回0
        *  该方法可能爆空指针异常，虽然并没有返回null！
        */
            if(0 != userService.selectCount(userId)) {
                User user = userService.getUser(userId);
            }else {
                User user = new User();
                user.setMonthlyFrequency(0);
                user.setUsername(userId);
                int i = userService.insertUser(user);
                if (i == 0) {
                    //新用户存入数据库，如果失败就重新登录
                    response.getWriter().write("账户信息无法储存到服务器，请尝试重新登录");
                    String casLogoutURL = "https://authserver.sicau.edu.cn/authserver/logout";
                    String redirectURL = casLogoutURL + "?service=" + URLEncoder.encode("https://carorder.sicau.edu.cn/SICAU/adminCar/index/home.html");
                    response.sendRedirect(redirectURL);
                }
            }
    }
}

